//
//  ViewController.m
//  15-UICollectionViewLifeCycle
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake((self.view.bounds.size.width - 5) / 2, 120);
    flow.minimumLineSpacing = 5;
    flow.minimumInteritemSpacing = 5;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
    
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
     NSLog(@"哈哈哈哈哈");
    
//    iOS10以前 3~5循环执行，cell完全离开屏幕时，出屏幕时返回，依旧进入重用吃3~4~5
//    以后出屏幕立即返回，4~5，提高了效率
}

//从重用池取出cell4
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    return cell;
}

//iOS8.0以后推出，cell将要显示在屏幕上5
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@-------%ld",cell,indexPath.row);
}
//已经离开6
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@-------%ld------[%@----%s]",cell,indexPath.row,self,__FUNCTION__);
}

#pragma mark --生命周期1
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
     NSLog(@"-----[%@----%s]",self,__FUNCTION__);
    return 1;
}
//2
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"-[%@----%s]",self,__FUNCTION__);
    return 90;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
